<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.Random" %>
<!DOCTYPE html>
<html>
<head>
    <title>注册新用户</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            background-color: #f2f2f2;
        }
        .container {
            width: 400px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            background-color: #fff;
        }
        .container h2 {
            text-align: center;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
        }
        .form-group input[type="text"],
        .form-group input[type="email"],
        .form-group input[type="password"],
        .form-group input[type="date"] {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .form-group input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .form-group input[type="submit"]:hover {
            background-color: #45a049;
        }
        .captcha {
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .captcha span {
            font-size: 18px;
            font-weight: bold;
        }
        .link {
            text-align: center;
            margin-top: 20px;
        }
        .link a {
            color: #0066cc;
            text-decoration: none;
        }
        .link a:hover {
            text-decoration: underline;
        }
        .instructions {
            text-align: center;
            margin-bottom: 20px;
        }
        .error {
            color: red;
            margin-top: -10px;
            margin-bottom: 10px;
            font-size: 14px;
        }
    </style>
    <script>
        function validateForm(event) {
            event.preventDefault();

            var formData = new FormData(event.target);
            var errorContainer = document.getElementById('error-container');

            var xhr = new XMLHttpRequest();
            xhr.open("POST", "RegisterServlet", true);
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var response = JSON.parse(xhr.responseText);
                    if (response.status === "error") {
                        errorContainer.textContent = response.message;
                    } else if (response.status === "success") {
                        errorContainer.textContent = "注册成功！";
                        errorContainer.style.color = "green";
                    }
                }
            };

            var params = [];
            formData.forEach(function (value, key) {
                params.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
            });
            xhr.send(params.join('&'));
        }
    </script>
</head>
<body>
    <%
        // 生成验证码
        Random rand = new Random();
        int captcha = 100000 + rand.nextInt(900000); // 生成六位数的验证码
        request.getSession().setAttribute("generatedCaptcha", String.valueOf(captcha)); // 将验证码保存到session中
    %>
    <div class="container">
        <div class="instructions">
            <p>Fill in the form to register in Yan Hospital.</p>
            <p>填写表格以在雁医院注册。</p>
        </div>
        <h2>注册新用户</h2>
        <form onsubmit="validateForm(event)">
            <div class="form-group">
                <h3>登录信息（必填）：</h3>
                <label for="email">账户（邮箱）：</label>
                <input type="email" id="email" name="email" required />

                <label for="password">密码：</label>
                <input type="password" id="password" name="password" required />

                <label for="confirmPassword">确认密码：</label>
                <input type="password" id="confirmPassword" name="confirmPassword" required />

                <div class="captcha">
                    <label for="enteredCaptcha">验证码：</label>
                    <span><%= captcha %></span>
                </div>
                <input type="text" id="enteredCaptcha" name="enteredCaptcha" required placeholder="输入上面的验证码" />
            </div>

            <div class="form-group">
                <h3>个人信息（必填）：</h3>
                <label for="name">姓名：</label>
                <input type="text" id="name" name="name" required />

                <label for="idNumber">身份证号码：</label>
                <input type="text" id="idNumber" name="idNumber" required />

                <label for="birthdate">出生日期：</label>
                <input type="date" id="birthdate" name="birthdate" required />

                <label for="phoneNumber">手机号码：</label>
                <input type="text" id="phoneNumber" name="phoneNumber" required />

                <label for="region">所属地区：</label>
                <input type="text" id="region" name="region" required />

                <label for="address">住址：</label>
                <input type="text" id="address" name="address" required />
            </div>

            <div class="error" id="error-container"></div>
            <div class="form-group">
                <input type="submit" value="注册" />
            </div>
        </form>
        <div class="link">
            <a href="login.jsp">已有账户？登录</a>
        </div>
    </div>
</body>
</html>
